package com.seegw.shaphar;

import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.javaweb.admin.entity.NcServiceLog;
import com.javaweb.admin.service.impl.NcServiceLogServiceImpl;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.CommonUtils;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.FileUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.namespace.QName;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Service
public class NCInterface {
    @Autowired
    private NcServiceLogServiceImpl ncServiceLogService;

    static String sServiceId = "E0BO01";
    static String sPwd = "";
    static String sSourceAppCode = "E0";
    static String sVersion = "";
    static String sXxlx = "";
    static String sSendtime = DateUtils.Now();
    static String xmlData = "";

    public static String SHA1_Hash(String xmlData)
    {
        StringBuilder sb = new StringBuilder();
        try {
            // 创建SHA实例
            MessageDigest md = MessageDigest.getInstance("SHA1");

            // 计算摘要
            byte[] digest = md.digest(xmlData.getBytes(Charset.forName("UTF-8")));

            // 将摘要转换为十六进制字符串

            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString().toUpperCase();
    }

    public String[] callESB(String YQNO,String billno, String postXmlData)
    {
        String[] rtn = new String[7];
        NcServiceLog log = new NcServiceLog();
        try {
            log.setSubmitNo(billno+"_"+YQNO);
            log.setRequestBody(postXmlData);
            log.setSubmitTime(DateUtils.now());

            rtn[0] = YQNO;
            rtn[1] = postXmlData;
            FileUtils.writeLog("NC接口日志" + YQNO, postXmlData);
            //

            JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
            QName operationName = new QName("http://webservice.esb.sphchina.com","receiveMessage");//如果有命名空间需要加上这个，第一个参数为命名空间名称，调用的方法名称
            String url = CommonConfig.ncServiceUrlKdl+"?wsdl";
            Client client = factory.createClient(url);
            client.getConduit().getTarget().getAddress().setValue(CommonConfig.ncServiceUrlKdl);
            Object[] res = null;
            res = client.invoke(operationName,postXmlData,sSendtime,sPwd,sServiceId,sSourceAppCode);
            String resultJson = res[0].toString();

            FileUtils.writeLog("NC接口日志" + YQNO, resultJson);
            rtn[2] = resultJson;
            JSONObject result = JSON.parseObject(resultJson);

            rtn[3] = DateUtils.getTime();
            rtn[4] = result.getString("result");
            rtn[5] = result.getString("msg");
            rtn[6] = "";
            log.setResponseBody(resultJson);
            log.setResponseCode(result.getString("result"));
            log.setResponseResult(result.getString("msg"));
            ncServiceLogService.save(log);
        }
        catch(Exception ex)
        {
            log.setResponseBody(ex.getMessage());
            log.setResponseCode("500");
            log.setResponseResult("");
            ncServiceLogService.save(log);
            rtn[6] = ex.getMessage();
        }
        if("Y".equals(rtn[4])){
            rtn[5] = "处理成功";
        }
        return rtn;
    }

}
